package timeitj.persistence;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.log4j.Logger;

/**
 * Entity class Project
 *
 * @author wallner
 */
@Entity
@NamedQuery(
name="ProjectsByOwner",
    query="select p from Project p WHERE p.owner= :projectOwner")
    
    @Table(name="PROJECT")
    public class Project implements Serializable {
    
    private static Logger logger = Logger.getLogger(Project.class.getName());
    
    @Id @GeneratedValue
    @Column(name="ID")
    private Long id;
    
    @Version
    @Column(name="OPT_LOCK")
    private Long version;
    
    @Column(name="PROJECTNAME")
    private String name;
    
    @Column(name="DESCRIPTION")
    private String description;
    
    @ManyToOne
    private Person owner;
    
    @OneToMany(mappedBy="project", cascade={CascadeType.REMOVE})
    private List<Invitation> invitations;
    
    @OneToMany(mappedBy = "project", cascade={CascadeType.REMOVE})
    private List<Participance> participances;
    
    public Project() {
        participances = new ArrayList<Participance>();
        invitations = new ArrayList<Invitation>();
    }
    
    /**
     * Gets the id of this Project.
     * @return the id
     */
    public long getId() {
        return this.id;
    }
        
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public Person getOwner() {
        return owner;
    }
    
    public void setOwner(Person owner) {
        logger.info("Made '" + owner.getLogin() + "' owner of project '" + name + "'.");
        this.owner = owner;
    }
    
    public List<Participance> getParticipances() {
        return participances;
    }
    
    public void setParticipances(List<Participance> participances) {
        this.participances = participances;
    }
    
    /**
     * Returns a hash code value for the object.  This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int)getId();
        return hash;
    }
    
    /**
     * Determines whether another object is equal to this Project.  The result is
     * <code>true</code> if and only if the argument is not null and is a Project object that
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Project)) {
            return false;
        }
        
        Project other = (Project)object;
        if (this.getId() != other.getId()) return false;
        return true;
    }
    
    /**
     * Returns a string representation of the object.  This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "timeitj.persistence.Project[id=" + getId() + "]";
    }
    
}